package com.javaclimb.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaclimb.music.dao.SongMapper;
import com.javaclimb.music.domain.Song;
import com.javaclimb.music.service.SongService;
import com.javaclimb.music.utils.PageUtils;
import com.javaclimb.music.utils.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper, Song> implements SongService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<Song> wrapper = new LambdaQueryWrapper<>();
        //根据歌手id查询
        if (StringUtils.isNotEmpty((CharSequence) params.get("singerId"))) {
            wrapper.eq(Song::getSingerId, params.get("singerId"));
        }
        //根据歌曲名模糊查询
        if (StringUtils.isNotEmpty((CharSequence) params.get("name"))) {
            wrapper.like(Song::getName, params.get("name"));
        }
        //是否进行分页
        boolean isPage = false;
        if (StringUtils.isNotBlank((CharSequence) params.get("isPage"))) {
            isPage = Boolean.parseBoolean(String.valueOf(params.get("isPage")));
        }
        PageUtils pageUtils = null;
        if (isPage) {
            IPage<Song> page = this.page(new Query<Song>().getPage(params), wrapper);
            pageUtils = new PageUtils(page);
        } else {
            List<Song> list = this.list(wrapper);
            pageUtils = new PageUtils(list, 0, 0, 0);
        }
        return pageUtils;
    }
}
